//
//  SHPEXmlParser.m
//  SHPEConferenceCompanion-iOS
//
//  Created by Richard Kirk on 3/24/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//


#import "SHPEXmlParser.h"
#import "Conference.h"
#import "Venue.h"
#import "Speaker.h"
#import "Session.h"
#import "Track.h"
#import "SessionType.h"




@implementation SHPEXmlParser
@synthesize xmlParser, xmlStringBuilder, dateFormatter;

-(id)init
{
    [super init];
    [xmlParser setDelegate:self];
    return self;
    
}


-(NSArray *)getAllConferences
{
    
    NSArray *arrayOfConferences = [[[NSArray alloc]init] autorelease];
    
    [xmlParser initWithContentsOfURL:[NSURL URLWithString:@"http://openshpe.staging.shpe.org/conference/v1/conferences"]];
    
    
    
    return arrayOfConferences;
    
    
}


/*
 
 -(void)parserDidStartDocument:(NSXMLParser *)parser
 {
 
 
 dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
 [dateFormatter setDateFormat:@"MM/dd/yyyy' 'HH:mm:ss"];
 
 
 
 
 
 
 
 }
 
 
 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
 {
 
 if ( ( [elementName isEqualToString:@"conference"] )) {
 self.id = [NSNumber numberWithInt:[[attributeDict objectForKey:@"id"] intValue]];
 self.name = [NSString stringWithString:[attributeDict objectForKey:@"name"]];
 self.thumbnailURL = [NSString stringWithString:[attributeDict objectForKey:@"xlink:logo"]];
 self.thumbnailData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.thumbnailURL]];
 return;
 }
 
 if ( ( [elementName isEqualToString:@"coordinate"] )) {
 
 self.location.lat = [NSNumber numberWithFloat:[[attributeDict objectForKey:@"lat"] floatValue]];
 self.location.lon = [NSNumber numberWithFloat:[[attributeDict objectForKey:@"lon"] floatValue]];
 
 return;
 }
 
 
 
 }
 
 
 
 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
 {   
 string = [string stringByReplacingOccurrencesOfString:@"&amp;" withString:@" "];
 if (!xmlStringBuilder) {
 xmlStringBuilder = [[NSMutableString alloc] initWithCapacity:100];
 }
 [xmlStringBuilder appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
 
 
 }
 
 -(void)parser:(NSXMLParser *)parser foundIgnorableWhitespace:(NSString *)whitespaceString
 {    
 
 //Didnt need this 
 
 
 NSLog(@"\nfound ignoreable whitespace: whitespace string: \"%@\"\n", whitespaceString);
 if (!xmlStringBuilder) {
 xmlStringBuilder = [[NSMutableString alloc] initWithCapacity:100];
 
 }
 //   if (whitespaceString != @"\n")
 // [xmlStringBuilder appendString:whitespaceString];
 
 

}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    
        // ignore root and empty elements
    if (( [elementName isEqualToString:@"location"]) ||
        ( [elementName isEqualToString:@"conference"] )) return;
    
    else if( [elementName isEqualToString:@"start-date"] ) {
        self.startDate = [dateFormatter dateFromString:xmlStringBuilder];
    }
    
    else if( [elementName isEqualToString:@"end-date"] ) {
        
        self.endDate = [dateFormatter dateFromString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"description"] ) {
        
        self.desc =[NSString stringWithString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"email"] ) {
        self.email = [NSString stringWithString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"website"] ) {
        self.website = [NSString stringWithString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"twitter"] ) {
        self.twitter = [NSString stringWithString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"address"] ) {
        self.location.address = [NSString stringWithString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"city"] ) {
        self.location.city = [NSString stringWithString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"province"] ) {
        self.location.province = [NSString stringWithString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"country"] ) {
        self.location.country = [NSString stringWithString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"postalCode"] ) {
        self.location.postalCode = [NSNumber numberWithInt:[xmlStringBuilder intValue]];
        
    }
    
    
    
    xmlStringBuilder = nil;
    
    
    
}



-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    
    NSLog(@"XML ERROR :\n%@\n",[parseError description]);
    
    UIAlertView *parserAlert = [[UIAlertView alloc]init];
    
    
    [parserAlert setTitle:@"Parsing Error!"];
    
    [parserAlert setMessage:[NSString stringWithFormat:@"Error %i, Description: %@, Line: %i, Column: %i", 
                             [parseError code], 
                             [parseError localizedDescription], 
                             [parser lineNumber], 
                             [parser columnNumber]]];
    
    [parserAlert addButtonWithTitle:@"OK"];
    [parserAlert show];
    [parserAlert release];
    
    
    
}

-(void)parserDidEndDocument:(NSXMLParser *)parser
{
    
    [dateFormatter release];
}




*/

@end
